public class BinarySearch {
    public int peakIndexInMountainArray1(int[] arr) {
        int n = arr.length;
        if (arr[0] > arr[1]) {
            return 0;
        }
        if (arr[n - 1] > arr[n - 2]) {
            return n - 1;
        }
        int left = 1, right = n - 2;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (arr[mid] >= arr[mid - 1] && arr[mid + 1] >= arr[mid]) {
                left = mid + 1;
            } else if (arr[mid] <= arr[mid - 1] && arr[mid + 1] <= arr[mid]) {
                right = mid - 1;
            } else {
                return mid;
            }
        }
        return left;
    }

    public int peakIndexInMountainArray2(int[] arr) {
        int n = arr.length;
        if (arr[0] > arr[1]) {
            return 0;
        }
        // if (arr[n - 1] > arr[n - 2]) {
        //     return n - 1;
        // }
        int left = 0, right = n - 1;
        while (left < right) {
            int mid = left + (right- left + 1) / 2;
            if (arr[mid] < arr[mid + 1]) {
                left = mid;
            } else if (arr[mid - 1] > arr[mid]) {
                right = mid - 1;
            } else {
                return mid;
            }
        }
        return 0;
    }

    public int peakIndexInMountainArray3(int[] arr) {
        int n = arr.length;
        int left = 0, right = n - 1;
        while (left < right) {
            int mid = left + (right - left + 1) / 2;
            if (arr[mid] < arr[mid + 1]) {
                left = mid;
            } else {
                right = mid - 1;
            }
        }
        return left + 1;
    }
}
